import { IArticle } from "./utils";

const code = /((```(\s*?\S*?)*?```)|((`(\s*?\S*?)*?`)))/g
// https://www.luogu.com.cn/webadmin/blog/article?status=%5B%5D&page=3
// let totpage = 
const domParser = new DOMParser();
function getTotPages(): Promise<number> {
    return new Promise((resolve, reject) => {
        $.get(`https://www.luogu.com.cn/webadmin/blog/article?status=%5B%5D`).then((data, status) => {
            if (status !== `success`) {
                reject(`Connect Error`);
                return;
            }
            let page = domParser.parseFromString(data, `text/html`);
            resolve(Number((page!.querySelector(`.am-u-sm-12 .pagination-centered ul`)!.lastElementChild!.firstElementChild! as HTMLLinkElement).href.split(`page=`)[1]))
        })
    })
}

function getArticleLinkList(page: number): Promise<string[]> {
    return new Promise((resolve, reject) => {
        $.get(`https://www.luogu.com.cn/webadmin/blog/article?status=%5B%5D&page=${page}`).then((data, status) => {
            const page = domParser.parseFromString(data, `text/html`);
            const blogsTr = page.querySelectorAll(`.am-u-sm-12 table tbody tr`)
            let articleList: string[] = []
            for (let now of blogsTr) {
                articleList.push((now.lastElementChild!.firstElementChild as HTMLLinkElement).href)
            }
            resolve(articleList);
        })
    })
}


